home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7958 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  38 lines

  1. Path: news.informatik.uni-muenchen.de!usenet
  2. From: Kurt Watzka <watzka@stat.uni-muenchen.de>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: about free()
  5. Date: Wed, 28 Feb 1996 15:40:31 +0100
  6. Organization: Institut fⁿr Statistik
  7. Message-ID: <3134695F.55E@stat.uni-muenchen.de>
  8. NNTP-Posting-Host: pc7.stat.uni-muenchen.de
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 2.0 (WinNT; I)
  13.  
  14. Chenyang Xu wrote:
  15. > Hi, there,
  16. >      I have a simple question here.
  17. >      I have a function say newarrary() which mallocs an array inside and
  18. > returns a pointer to this arrary. In another function foo.c, the result
  19. > of newarray() is assigned to a pointer ptr. Now after some operations on
  20. > this array. I use free(ptr) to free the memory allocated by newarrary().
  21. > My question is whould this cause a problem, since free() is not freeing
  22. > the same pointer which is allocated by the malloc() although this
  23. > pointer point to the same memory.
  24.  
  25. 1.) You should _never_ free a pointer that was not allocated through one
  26.     of the memory allocation function.
  27.  
  28. 2.) Whether you use the variable that held the "pointer" returned from the
  29.     allocation function or another variable that _holds_ _the_ _same_
  30.     _value_ does not matter at all. Like all other C functions, free()
  31.     will get it's argument passed by value, so it has access to the "value"
  32.     of your pointer, not to the variable that holds this value.
  33.  
  34. Kurt
  35.